package cn.skidz.im.ui.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.skidz.im.R;
import cn.skidz.im.model.GroupMember;
import cn.skidz.im.ui.view.SettingItemView;
import cn.skidz.im.ui.widget.SelectableRoundedImageView;
import cn.skidz.im.ui.widget.switchbutton.SwitchButton;
import cn.skidz.im.utils.ImageLoaderUtils;
import io.rong.imkit.utils.RongDateUtils;

public class GroupMuteListAdapter extends BaseAdapter {

    private List<GroupMember> datas = new ArrayList<>();
    private OnGroupMuteListener listener;

    public void updateList(List<GroupMember> datas) {
        if (datas == null) {
            return;
        }
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void setOnGroupManagerListener(OnGroupMuteListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas == null ? null : datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(parent.getContext(), R.layout.group_mute_item, null);
            holder.groupMutePortrait = convertView.findViewById(R.id.group_mute_portrait);
            holder.groupMuteTitle = convertView.findViewById(R.id.group_mute_title);
            holder.groupMuteSwitch = convertView.findViewById(R.id.group_mute_switch);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GroupMember groupMember = datas.get(position);
        String groupNickName = groupMember.getGroupNickName();
        if (!TextUtils.isEmpty(groupNickName)) {
            holder.groupMuteTitle.setText(groupNickName);
        } else {
            holder.groupMuteTitle.setText(groupMember.getName());
        }
        if (!TextUtils.isEmpty(groupMember.getPortraitUri())) {
            ImageLoaderUtils.displayGroupPortraitImage(groupMember.getPortraitUri(), holder.groupMutePortrait);
        }
        holder.groupMuteSwitch.setChecked(groupMember.getMemberMute() == 1);
        holder.groupMuteSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (this.listener != null) {
                this.listener.onSelected(position, isChecked);
            }
        });
        return convertView;
    }

    class ViewHolder {
        SelectableRoundedImageView groupMutePortrait;
        TextView groupMuteTitle;
        SwitchButton groupMuteSwitch;
    }

    public interface OnGroupMuteListener {
        void onSelected(int position, boolean selected);
    }
}
